package com.example.its2021606134chh.TrafficQueryModule;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TAFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TAFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private BarChart barChatTA;
    private int[] roadList = {1, 2, 3, 4, 5, 6, 7};

    public TAFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TAFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TAFragment newInstance(String param1, String param2) {
        TAFragment fragment = new TAFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_t_a, container, false);
        barChatTA = (BarChart) view.findViewById(R.id.barChatTA);
        // Inflate the layout for this fragment
        barChatTA.setNoDataText("正在加载数据...");
//        x
        List<String> xLabel = new ArrayList<>();
        xLabel.add("周一");
        xLabel.add("周二");
        xLabel.add("周三");
        xLabel.add("周四");
        xLabel.add("周五");
        xLabel.add("周六");
        xLabel.add("周日");
        XAxis xAxis = barChatTA.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xLabel));
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum(7f);
        xAxis.setCenterAxisLabels(true);

//        y
        barChatTA.getAxisRight().setEnabled(false);
        List<String> yLabel = new ArrayList<>();
        yLabel.add("畅通");
        yLabel.add("缓行");
        yLabel.add("一般拥堵");
        yLabel.add("中度拥堵");
        yLabel.add("严重拥堵");
        YAxis yAxis = barChatTA.getAxisLeft();
        yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        yAxis.setValueFormatter(new IndexAxisValueFormatter(yLabel));
        yAxis.setMinWidth(0f);
        yAxis.setMaxWidth(5f);
        yAxis.setGranularity(1f);
        yAxis.setLabelCount(5, false);
        yAxis.setCenterAxisLabels(true);
        barChatTA.setDrawGridBackground(false);

//        legend
        Legend legend = barChatTA.getLegend();
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setFormSize(8);
        legend.setTextSize(8);

        final ArrayList<BarEntry> entries1 = new ArrayList<>();
        final ArrayList<BarEntry> entries2 = new ArrayList<>();
        final ArrayList<BarEntry> entries3 = new ArrayList<>();
        final ArrayList<BarEntry> entries4 = new ArrayList<>();
        final ArrayList<BarEntry> entries5 = new ArrayList<>();
        final ArrayList<BarEntry> entries6 = new ArrayList<>();
        final ArrayList<BarEntry> entries7 = new ArrayList<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int road : roadList) {
                        for (int i = 1; i < 8; i++) {
                            String json = HttpUtil.syncRequest("get_road_status", new String[]{"RoadId", "UserName"}, road,"user1");
                            RoadStatus status = new Gson().fromJson(json, RoadStatus.class);
                            switch (road) {
                                case 1:
                                    entries1.add(new BarEntry(i, Integer.parseInt(status.getStatus())));
                                    break;
                                case 2:
                                    entries2.add(new BarEntry(i, Integer.parseInt(status.getStatus())));
                                    break;
                                case 3:
                                    entries3.add(new BarEntry(i, Integer.parseInt(status.getStatus())));
                                    break;
                                case 4:
                                    entries4.add(new BarEntry(i, Integer.parseInt(status.getStatus())));
                                    break;
                                case 5:
                                    entries5.add(new BarEntry(i, Integer.parseInt(status.getStatus())));
                                    break;
                                case 6:
                                    entries6.add(new BarEntry(i, Integer.parseInt(status.getStatus())));
                                    break;
                                case 7:
                                    entries7.add(new BarEntry(i, Integer.parseInt(status.getStatus())));
                                    break;
                                default:
                                    break;
                            }

                        }
                    }
                    BarDataSet barDataSet1 = new BarDataSet(entries1, "学院路");
                    BarDataSet barDataSet2 = new BarDataSet(entries2, "联想路");
                    BarDataSet barDataSet3 = new BarDataSet(entries3, "医院路");
                    BarDataSet barDataSet4 = new BarDataSet(entries4, "幸福路");
                    BarDataSet barDataSet5 = new BarDataSet(entries5, "环城快速路");
                    BarDataSet barDataSet6 = new BarDataSet(entries6, "环城高速路");
                    BarDataSet barDataSet7 = new BarDataSet(entries7, "停车场");
                    barDataSet1.setColor(Color.parseColor("#C23531"));
                    barDataSet2.setColor(Color.parseColor("#2F4554"));
                    barDataSet3.setColor(Color.parseColor("#61A0A8"));
                    barDataSet4.setColor(Color.parseColor("#D48265"));
                    barDataSet5.setColor(Color.parseColor("#91C7AE"));
                    barDataSet6.setColor(Color.parseColor("#749F83"));
                    barDataSet7.setColor(Color.parseColor("#CA8622"));
                    final BarData barData = new BarData(barDataSet1, barDataSet2, barDataSet3,
                            barDataSet4, barDataSet5, barDataSet6, barDataSet7);
                    barData.setDrawValues(false);
                    float groupSpace = 0.2f;
                    float barWidth = (1f - groupSpace) / 7;
                    float barSpace = 0.0f;
                    barData.setBarWidth(barWidth);
                    barData.groupBars(0f, groupSpace, barSpace);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            barChatTA.setData(barData);
                            barChatTA.invalidate();
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return view;
    }

    private void initView() {

    }
}